<template>
  <div class="point-red-container">
    <h1 class="coupon-title">
      您可以兑换{{point_name}}为:
      <span class="points">{{points}}</span>
    </h1>
    <div class="coupon-main" v-if="redPacketList.length>0">
      <ul>
        <van-list
          v-model="loading"
          :finished="finished"
          :error.sync="error"
          error-text="请求失败，点击重新加载"
          finished-text=""
          @load="onLoad"
        >
          <li v-for="(item,index) in redPacketList" :key="index">
            <div class="coupons-item clearfix">
              <section class="price-box">
                <span class="price">{{mSymbol.name}}<em>{{item.amount}}</em></span>
              </section>
              <section class="coupon-left">
                <h2>{{item.title}}</h2>
                <p class="txt">{{item.wishing}}</p>
              </section>
              <section class="coupon-right">
                <span class="coupon-point">
                  <i class="points">{{item.points}}</i>{{point_name}}
                </span>
                <div v-if="points-item.points>=0">
                  <van-button class="confrim-btn disabled" color="#EDEDED" v-if="item.is_exchange==0" round >立即兑换</van-button>
                  <van-button class="confrim-btn" v-else @click="clickExchange(item.point_red_pack_id)" color="linear-gradient(to right, #F82337, #FF2D59)" round >立即兑换</van-button>
                </div>
                <div v-else>
                  <van-button class="confrim-btn disabled" color="#ccc" round >{{point_name}}不足</van-button>
                </div>
              </section>
            </div>
          </li>
        </van-list>
      </ul>
    </div>
    <div v-else>
      <no-data :noData="noData"></no-data>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Dialog } from 'vant'
import { getPointRedInfo, exchangeRed } from '@/api/user/member/memberApi'
import noData from '@/components/noData/index'
// import noDataImg from '@img/no_collection_goods.png'
import { mapGetters } from 'vuex'

export default Vue.extend({
  name: 'index',
  components: {
    noData
  },
  data() {
    return {
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_collection_goods.png',
        text: '暂无数据'
      },
      point_name: '积分',
      points: '',
      code: '',
      redPacketList: [
        /* {
          point_red_pack_id: '111',
          title: '红包名称',
          amount: '239',
          wishing: '祝福语',
          points: '22',
          is_exchange: '1'
        } */
      ],
      loading: false,
      finished: false,
      error: false,
      p: 2,
      id: ''
    }
  },
  methods: {
    init() {
      if (this.$route.query.id) {
        this.id = this.$route.query.id
      }
      this.$loadingWrap.show()
      setTimeout(() => {
        this.$loadingWrap.close()
      }, 10000)
      const params = { id: this.id }
      getPointRedInfo(params).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.points = resData.points
          this.redPacketList = resData.list
          this.point_name = resData.point_name
          const pageTitle = this.point_name + '兑换红包'
            wx.setNavigationBarTitle({
             title: pageTitle
            })
          this.code = resData.code
          this.p=2
        } else {
          this.$Error(res.msg)
        }
      })
    },
    // 点击兑换
    clickExchange(id) {
      const params = {
        id: id,
        code: this.code
      }
      Dialog.confirm({
        message: '确定兑换吗？',
        className: 'common-confirm',
        closeOnClickOverlay: true
      }).then(() => {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 10000)
        exchangeRed(params).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.$Error('兑换成功')
            this.init()
          } else {
            this.$Error(res.msg)
            this.init()
          }
        })
        // on confirm
      }).catch(() => {
        // on cancel
      })
    },
    onLoad() {
      this.loading = true
      const data = {
        p: this.p,
        id: this.id
      }
      getPointRedInfo(data)
        .then(res => {
          this.loading = false
          if (res.data.list.length) {
            this.redPacketList.push(...res.data.list)
            this.p++
          } else {
            this.finished = true
          }
        })
        .catch(() => {
          this.error = true
        })
    }
  },
  created() {
    this.init()
  },
  mounted() {
    // 小程序触底加载更多
    window.addEventListener('reachbottom', () => {
      // console.log('reachbottom')
      this.onLoad()
    })
  },
  computed: {
    // 获取自定义符号
    ...mapGetters(['mSymbol'])
  }
})
</script>

<style lang="scss">
@import '../../../../styles/mixin';
.point-red-container{
  padding:25px;
  .coupon-title{
    margin:16px 0 30px;
    font-size:28px;
    color: #666666;
    .points{
      font-weight:bold;
      color:#FA253F;
    }
  }
  .coupon-main{
    li{
      margin-bottom:30px;
    }
    .coupons-item{
      width:700px;
      height:220px;
      // padding:46px 0 40px 0;
      background: url(https://img.wifenxiao.com/h5-wfx/images/user/point/red_packet_bg.png) no-repeat;
      background-size:100% 100%;
      display: flex;
      .price-box{
        width: 180px;
        padding-left:10px;
        text-align: center;
        .price{
          font-weight:bold;
          color:#fff;
          // font-size: 28px;
          // TODO 定义了lineClamp就不需要再定义font-size 和line-height了 否则在小程序会显示不出来
          @include lineClamp(28px,220px,1);
          em{
            display:inline;
            font-weight:bold;
            font-size:50px;
          }
        }
      }
      .coupon-left{
        width:320px;
        height:100%;
        display:flex;
        flex-direction: column;
        justify-content: center;
        padding-left:20px;
        h2{
          font-size:32px;
          font-weight: bold;
          @include showEllipsis;
        }
        .txt{
          margin-top: 30px;
          font-size:20px;
          @include showEllipsis;
        }
      }
      .coupon-right{
        width: 180px;
        padding:20px 0;
        height:100%;
        display:flex;
        flex-direction: column;
        justify-content: center;
        text-align:center;
      }
      .coupon-point{
        font-size:24px;
        color:#FFA101;
        margin-bottom: 30px;
        .points{
          font-size:32px;
          font-weight:bold;
        }
      }
      .confrim-btn{
        display: block;
        width: 126px;
        height: 46px;
        line-height: 46px;
        font-size: 24px;
        margin: 1px auto;
        &.disabled{
          color:#B3B3B3!important;
        }
      }
    }
  }
}
</style>
